package com.jt.sp02.controller;

import com.jt.sp01.pojo.Item;
import com.jt.sp01.service.ItemService;
import com.jt.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
public class Itemcontroller {
    @Autowired
    private ItemService itemService;

    @GetMapping("getItems/{orderId}")
    public JsonResult getItems(@PathVariable("orderId") String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);
        if (Math.random() < 1) {
            int t = new Random().nextInt(5000);
            log.info("阻塞时间+" + t);
            Thread.sleep(t);
        }
        return JsonResult.ok(items);
    }

    @PostMapping("decreaseNumbers")
    public JsonResult decreaseNumbers(@RequestBody List<Item> list) {
        itemService.decreaseNumbers(list);
        return JsonResult.ok();
    }

    @GetMapping("/favicon.ico")
    public void ico() {

    }
}
